package com.shiyipicture.shiyipicturebackend.api.aliyun;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.shiyipicture.shiyipicturebackend.api.aliyun.model.CreateOutPaintingTaskRequest;
import com.shiyipicture.shiyipicturebackend.api.aliyun.model.CreateOutPaintingTaskResponse;
import com.shiyipicture.shiyipicturebackend.api.aliyun.model.GetOutPaintingTaskResponse;
import com.shiyipicture.shiyipicturebackend.exception.BusinessException;
import com.shiyipicture.shiyipicturebackend.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AliYunAiApi {

  @Value("${aliYunAi.apiKey}")
  private String apiKey;
  // 创建任务地址
  public static final String CREATE_OUT_PAINTING_TASK_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting";

  // 查询任务状态
  public static final String GET_OUT_PAINTING_TASK_URL = "https://dashscope.aliyuncs.com/api/v1/tasks/%s";

  /**
   * 创建任务请求
   *
   * @param createOutPaintingTaskRequest
   * @return
   */
  public CreateOutPaintingTaskResponse createOutPaintingTask(CreateOutPaintingTaskRequest createOutPaintingTaskRequest) {
    if (createOutPaintingTaskRequest == null) {
      throw new BusinessException(ErrorCode.OPERATION_ERROR, "扩图参数为空");
    }

    HttpRequest httpRequest = HttpRequest.post(CREATE_OUT_PAINTING_TASK_URL)
      .header(Header.AUTHORIZATION, "Bearer " + apiKey)
      .header("X-DashScope-Async", "enable")
      .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
      .body(JSONUtil.toJsonStr(createOutPaintingTaskRequest));

    try (HttpResponse httpResponse = httpRequest.execute()) {
      if (!httpResponse.isOk()) {
        log.error("请求异常：{}", httpResponse.body());
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 扩图失败");
      }
      CreateOutPaintingTaskResponse createOutPaintingTaskResponse = JSONUtil.toBean(httpResponse.body(), CreateOutPaintingTaskResponse.class);
      String errorCode = createOutPaintingTaskResponse.getCode();
      if (StrUtil.isNotBlank(errorCode)) {
        String errorMessage = createOutPaintingTaskResponse.getMessage();
        log.error("AI 扩图失败，errorCode:{}, errorMessage:{}", errorCode, errorMessage);
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 扩图接口响应异常");
      }
      return createOutPaintingTaskResponse;
    }
  }

  /**
   * 查询创建的任务
   * @param taskId
   * @return
   */
  public GetOutPaintingTaskResponse getOutPaintingTask(String taskId) {
    if (StrUtil.isBlank(taskId)) {
      throw new BusinessException(ErrorCode.OPERATION_ERROR, "任务 id 不能为空");
    }
    try (HttpResponse httpResponse = HttpRequest.get(String.format(GET_OUT_PAINTING_TASK_URL, taskId))
      .header(Header.AUTHORIZATION, "Bearer " + apiKey)
      .execute()) {
      if (!httpResponse.isOk()) {
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "获取任务失败");
      }
      return JSONUtil.toBean(httpResponse.body(), GetOutPaintingTaskResponse.class);
    }
  }

}
